# -*- coding: utf-8 -*-
from CSVTools import CSVData
from Rebides import RebidesDB
#from BeautifulSoup import BeautifulSoup

class RebideStats:
    def __init__( self ):
        pass
    
    def CSVStatistics( self, your, databaseName, filename ):

        dataRows = RebidesDB().loadDatabaseRebides( databaseName )
        data = dataRows[ 0 ]
        names = dataRows[ 1 ]
        acad_years = dataRows[ 2 ]
        categorys = dataRows[ 3 ]
        grades = dataRows[ 4 ]
        
        #title = "Number of teachers in the higher education system per establishment per year"
        stats = []
        count = 0
        
        for i in range( 0, len( names ) ):#2 x 11 x 400 000
            subdata = filter( lambda x: x[ 8 ] == names[ i ] , data )

            for j in range( 0, len( acad_years ) ):
                stats.append( [ names[ i ] ] )
                stats[ count ].append( acad_years[ j ] )
                stats[ count ].append( len( filter( lambda x: x[ 6 ] == acad_years[ j ], subdata ) ) )
                count += 1
                pass
            pass
        CSVData().writeCSV(filename + "_1", stats)
        
        
        #title = "Number of teachers divided per categories per establisment and per year"
        stats = []
        count = 0
        for i in range(0, len(names)):
            subdata = filter( lambda x: x[ 8 ] == names[ i ] , data )
            
            for j in range(0, len(acad_years)):
                aux = filter( lambda x: x[ 6 ] == acad_years[ j ], subdata )
                
                for k in range(0, len(categorys)):
                    stats.append( [ names [ i ] ] )
                    stats[ count ].append(acad_years[ j ])
                    stats[ count ].append(categorys[ k ] )
                    stats[ count ].append( len( filter( lambda x: x[ 4 ] == categorys[ k ], aux ) ) )
                    count += 1
                pass
            pass
        CSVData().writeCSV(filename + "_2", stats)
        
        
        #title = "Number of teachers divided per type of degree per establishment and per year)"
        stats = []
        count = 0
        
        for i in xrange(0, len( names ) ):
            subdata = filter( lambda x:  x[ 8 ] == names[ i ] , data )
            
            for j in xrange(0, len( acad_years )):
                aux = filter( lambda x:  x[ 6 ] == acad_years[ j ] , subdata )
                
                for k in xrange(0, len( grades ) ):
                    stats.append( [ names [ i ] ] )
                    stats[ count ].append( acad_years[ j ] )
                    stats[ count ].append( grades[ k ] )
                    stats[ count ].append( len( filter( lambda x: x[ 2 ] == grades[ k ] , aux ) ) )
                    count += 1
                pass
            pass

        CSVData().writeCSV( filename + "_3", stats )
        pass

    def HTMLStatistics( self, your, databaseName, filename ):

        dataRows = RebidesDB().loadDatabaseRebides(databaseName)
        data = dataRows[ 0 ]
        stabNames = dataRows[ 1 ]
        acad_years = dataRows[ 2 ]
        categorys = dataRows[ 3 ]
        grades = dataRows[ 4 ]
        
        title = "Teachers in the higher education system per establishment per year"
        stats = []
        count = 0
        
        for i in range(0, len( stabNames ) ):
            subdata = filter( lambda x: x[ 8 ] == stabNames[ i ] , data )
            
            for j in range(0, len( acad_years) ):
                aux = filter( lambda x: x[ 6 ] == acad_years[ j ], subdata )
                
                for k in range(0, len( aux ) ):
                    stats.append( [ stabNames[ i ] ] )
                    stats[ count ].append( acad_years[ j ] )
                    stats[ count ].append( aux[ k ][ 1 ] )
                    count += 1
                    pass
                pass
            pass
        pass
        pageHTML = self.tableHTML( title, stats )
        
        title = "Teachers divided per categories per establisment and per year"
        stats = []
        count = 0
        
        for i in range(0, len( stabNames ) ):
            subdata = filter( lambda x: x[ 8 ] == stabNames[ i ] , data )
            
            for j in range(0, len( acad_years ) ):
                aux = filter( lambda x: x[ 6 ] == acad_years[ j ] , subdata )
                
                for k in range(0, len( categorys) ):
                    aux2 = filter( lambda x: x[ 4 ] == categorys[ k ], aux )
                    
                    for l in range(0, len( aux2 ) ):
                        stats.append( [ stabNames[ i ] ] )
                        stats[ count ].append( acad_years[ j ] )
                        stats[ count ].append( categorys[ k ] )
                        stats[ count ].append( aux2[ l ][ 1 ] )
                        count += 1
                        pass
                    pass
                pass
            pass
        pass
        pageHTML += self.tableHTML( title, stats )

        title = "Teachers divided per type of degree per establishment and per year"
        stats = []
        count = 0
        for i in range( 0, len( stabNames ) ):
            subdata = filter( lambda x: x[ 8 ] == stabNames[ i ] , data )
            
            for j in range(0, len( acad_years) ):
                aux = filter( lambda x: x[ 6 ] == acad_years[ j ] , subdata )
                
                for k in range(0, len( grades ) ):
                    aux2 = filter( lambda x: x[ 2 ] == grades[ k ] , aux)
                    
                    for l in range(0, len( aux2 ) ):
                        stats.append( [ stabNames[ i ] ] )
                        stats[ count ].append( acad_years[ j ] )
                        stats[ count ].append( grades[ k ] )
                        stats[ count ].append( aux2[ l ][ 1 ] )
                        count += 1
                        pass
                    pass
                pass
            pass
        pass
        pageHTML += self.tableHTML( title, stats )
        self.makeHTMLPage( filename + ".html", "Statistics", pageHTML )
    pass


    def makeHTMLPage( self, name, title, bodyHTML ):
        ficheiro = file( name, "w" )

        beginHTML = "<html><head><title>%s</title></head><body>" % ( title )
        endHTML = "</body></html>"
        
        pageHTML = beginHTML + bodyHTML + endHTML
        #pageHTML = BeautifulSoup( ''.join( pageHTML ) )
        #pageHTML = pageHTML.prettify()
        ficheiro.write( pageHTML )
        ficheiro.close()
        pass
    pass

    #http://www.w3schools.com/html/html_tables.asp
    def tableHTML( self, title, data ):
        title = '''<font size="3" color="red" face="verdana"> %s </font>''' % ( title )
        table = "<table border = 2>"
        
        for i in range(0, len ( data ) ):
            table += "<tr>"
            for j in range(0, len( data[ i ] ) ):
                table += "<td> %s </td>" % ( data[ i ][ j ] )
                pass
            table += "</tr>"
            pass
        table += "</table><br><br><br>"
        return title + table
        pass